﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace proLearningEnglish
{
    public partial class frmComposeQuestion : Form
    {
        private int questionType;
        private List<SimpleWord> lstWordSelected = null;
        private List<WordSense> lstWordSense = null;
        public frmComposeQuestion(int qType)
        {
            InitializeComponent();
            this.questionType = qType;
            this.lstWordSelected = new List<SimpleWord>();
            if (this.questionType == 2)
                this.lstWordSense = new List<WordSense>();
        }

        public frmComposeQuestion(Question q)
        {
            InitializeComponent();
            this.questionType = q.getTypeInt();
            this.lstWordSelected = new List<SimpleWord>();
            if (this.questionType == 2)
                this.lstWordSense = new List<WordSense>();
        }

        private void frmComposeQuestion_Load(object sender, EventArgs e)
        {
            this.labSense.Text = "";
            this.treWord.CheckBoxes = true;
            this.loadWord();
            TabPage tabpage = this.tabControl.TabPages[this.questionType - 1];
            foreach (TabPage tab in this.tabControl.TabPages)
                if (tab != tabpage)
                    tab.Dispose();
            //this.tabControl.TabPages[questionType - 1].Show();
        }

        private void loadWord()
        {
            this.treWord.Nodes.Clear();
            List<SimpleWord> lst = frmMain.objDataManager.getIsARootSimpleWordNodes();
            TreeNode rootNode = new TreeNode("Vocabulary");
            this.treWord.Nodes.Add(rootNode);
            foreach (SimpleWord word in lst)
            {
                addNode(rootNode, word);
            }
            this.treWord.ExpandAll();
        }

        private void addNode(TreeNode node, SimpleWord objSWord)
        {
            TreeNode nodeTmp = node.Nodes.Add(objSWord.getID(), objSWord.getLabel() + " (" + WordSense.WORDTYPESHORTLIST[objSWord.getSense().getTypeInt()] + ") : " + objSWord.getSense().getLabel());
            nodeTmp.Tag = objSWord;
            List<SimpleWord> lst = frmMain.objDataManager.getIsAChildSimpleWordNodes(objSWord);
            for (int i = 0; i < lst.Count; i++)
            {
                addNode(nodeTmp, lst[i]);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {

        }

        private void treWord_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
        }

        private void treWord_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            e.Node.Checked = !e.Node.Checked;
            if (e.Node != this.treWord.TopNode)
            {
                SimpleWord word = (SimpleWord)e.Node.Tag;      
                switch(this.questionType)
                {
                    case 1:
                        {
                            if (e.Node.Checked)
                            {

                                int i = this.lstWord.Items.Add(word.getLabel());
                                this.lstWordSelected.Add(word);
                                //this.lstWord.SelectedIndex = i;
                            }
                            else
                            {
                                this.lstWord.Items.Remove(word.getLabel());
                                this.lstWordSelected.Remove(word);
                                this.picSense.Image = null;
                            }
                            break;
                        }
                    case 2:
                        {
                            if (e.Node.Checked)
                            {
                                int i = this.lstWord2.Items.Add(word.getLabel());
                                this.lstWordSelected.Add(word);
                                this.lstWord2.SelectedIndex = i;
                                lstWord2_SelectedIndexChanged(sender, e);
                            }
                            else
                            {
                                this.lstWord2.Items.Remove(word.getLabel());
                                this.lstWordSelected.Remove(word);
                                this.clbPicture.Items.Clear();
                                this.clbPicture.Tag = null;
                                this.pictureBox2.Image = null;
                            }
                            break;
                        }
                }
            }
        }

        private void lstWord_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lstWord.SelectedIndex > -1)
            {
                WordSense sense = ((SimpleWord)this.lstWordSelected[this.lstWord.SelectedIndex]).getSense();
                labSense.Text = sense.getLabel();
                String strPic = sense.getPicture();
                if (File.Exists(frmMain.strPathPicture + strPic))
                {
                    this.picSense.Image = Image.FromFile(frmMain.strPathPicture + strPic);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            switch(this.questionType)
            {
                case 1:
                    {
                        int maxIndex = frmMain.objDataManager.getMaxIndexQuestion();
                        Question q = new Question("Câu_hỏi_" + (maxIndex + 1).ToString(), this.questionType, DateTime.Today.ToShortDateString());
                        frmMain.objDataManager.addQuestion(q);
                        frmMain.objDataManager.addRelation(q, this.lstWordSelected);
                        frmMain.objDataManager.save();
                        MessageBox.Show("Tạo câu hỏi mới thành công!", "Thành công", MessageBoxButtons.OK);
                        break;
                    }
                case 2:
                    {
                        int maxIndex = frmMain.objDataManager.getMaxIndexQuestion();
                        Question q = new Question("Câu_hỏi_" + (maxIndex + 1).ToString(), this.questionType, DateTime.Today.ToShortDateString());
                        frmMain.objDataManager.addQuestion(q);
                        foreach (SimpleWord sw in this.lstWordSelected)
                        {
                            frmMain.objDataManager.addRelation(q, new Word(sw.getLabel()));
                        }
                        frmMain.objDataManager.addRelation(q, this.lstWordSense);
                        frmMain.objDataManager.save();
                        MessageBox.Show("Tạo câu hỏi mới thành công!", "Thành công", MessageBoxButtons.OK);
                        break;
                    }
                default:
                    break;
            }
        }
        
        private void lstWord2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lstWord2.SelectedIndex >= 0)
            {
                this.clbPicture.Items.Clear();
                this.clbPicture.Tag = null;
                SimpleWord word = this.lstWordSelected[this.lstWord2.SelectedIndex];
                List<SimpleWord> lstChild = frmMain.objDataManager.getIsAChildSimpleWordNodes(word);
                this.clbPicture.Tag = lstChild;
                foreach (SimpleWord s in lstChild)
                {
                    this.clbPicture.Items.Add(s.getSense().getPicture());
                }
            }
        }

        private void clbPicture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.clbPicture.SelectedIndex >= 0)
            {
                string strPath = frmMain.strPathPicture + ((List<SimpleWord>)this.clbPicture.Tag)[this.clbPicture.SelectedIndex].getSense().getPicture();
                if (File.Exists(strPath))
                {
                    this.pictureBox2.Image = Image.FromFile(strPath);
                }
                else
                {
                    this.pictureBox2.Image = null;
                }
            }
        }

        private void clbPicture_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.Index >= 0)
            {
                WordSense s = ((List<SimpleWord>)this.clbPicture.Tag)[e.Index].getSense();
                if (e.NewValue == CheckState.Unchecked)
                {                    
                    this.lstWordSense.Remove(s);
                    //Console.WriteLine("Remove: " + s.getLabel());
                }
                else
                {
                    this.lstWordSense.Add(s);
                    //Console.WriteLine("Add: " + s.getLabel());
                    //Console.WriteLine("Count: " + this.lstWordSense.Count.ToString());
                }
            }
        }
    }
}
